package com.zyzy.leftfourdead.fragment.homepager;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zyzy.leftfourdead.R;
import com.zyzy.leftfourdead.adapter.dairyfoodadapter.DairyFragmentAdapter;
import com.zyzy.leftfourdead.adapter.dairyfoodadapter.DairyFragmentTopAdapter;
import com.zyzy.leftfourdead.bean.DairyBean;
import com.zyzy.leftfourdead.helper.MyListView;
import com.zyzy.leftfourdead.helper.OkHttpClientHelper;
import com.zyzy.leftfourdead.helper.UrlHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by loverforever on 2016/8/20.
 * dairyfood 乳品
 */
public class DairyFoodFragment extends Fragment {
    private static final String TAG = "WaterFragment";
    //    上下文对象
    private Context context = null;
    //    定义控件
    private ViewPager viewPager_waterFragment;
    private MyListView listView_waterFragment;
    private PullToRefreshScrollView scrollView_waterFragment;

    //    定义接口地址
    private String urlString;

    //    广告栏 list集合
    private List<DairyBean.BannerBean> topList = new ArrayList<>();

    //    商品列表 list集合
    private List<DairyBean.ProductsBean> totalList = new ArrayList<>();

    //    定义商品列表适配器
    private DairyFragmentAdapter adapter;

    //定义上面广告栏的适配器
    private DairyFragmentTopAdapter waterFragmentTopAdapter;
    //定义handler
    private Handler handler = new Handler();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        初始化上下文对象
        context = getContext();


//        okttp加载网络数据
        loadNetworkData();
        //        初始化适配器


    }

    /**
     * okttp加载网络数据
     */
    //    加载网络数据
    public void loadNetworkData() {
        urlString = UrlHelper.URL_DAIRY;
        //  格式化获取地址
        adapter = new DairyFragmentAdapter(context, totalList);
        waterFragmentTopAdapter = new DairyFragmentTopAdapter(context, topList);
//        开启子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String json = OkHttpClientHelper.getStringFromURL(getContext(), urlString, "WATER");

                    DairyBean waterBean = adapter.parseJsonToWaterBean(json);
                    topList = waterBean.getBanner();
                    totalList = waterBean.getProducts();

                    handler.post(new Runnable() {
                        @Override
                        public void run() {

                            adapter.reloadListView(totalList, true);
                            waterFragmentTopAdapter.reloadListView(topList, true);

                            Log.i(TAG, "===----====topList: "+topList.toString());
                            Log.i(TAG, "===----====topList: "+totalList.toString());
                            scrollView_waterFragment.onRefreshComplete();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//       填充布局
        View view = inflater.inflate(R.layout.fragment_water_homepage, container, false);
//        初始化控件
        viewPager_waterFragment = (ViewPager) view.findViewById(R.id.viewPager_waterFragment);
        listView_waterFragment = (MyListView) view.findViewById(R.id.listView_waterFragment);
        scrollView_waterFragment = (PullToRefreshScrollView) view.findViewById(R.id.scrollView_waterFragment);

        viewPager_waterFragment.setOffscreenPageLimit(4);


        viewPager_waterFragment.setAdapter(waterFragmentTopAdapter);
        listView_waterFragment.setAdapter(adapter);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        //设置既可以下拉刷新，又可以上拉加载更多
        scrollView_waterFragment.setMode(PullToRefreshScrollView.Mode.BOTH);

        //设置监听器
        scrollView_waterFragment.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                    loadNetworkData();

                // 重新加载网络数据
            }
//            下拉屏幕

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {

                Toast.makeText(context, "无更多数据", Toast.LENGTH_SHORT).show();

            }
//            上滑屏幕
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkHttpClientHelper.cancelCall("WATER");
    }

}
